<template>
  <ContentWrap>
    <el-button type="primary" @click="add">
      <Icon class="mr-5px" icon="ep:plus" />
      添加
    </el-button>
  </ContentWrap>
  <ContentWrap>
    <el-table :data="list">
      <el-table-column align="center" prop="id" label="ID" />
      <el-table-column align="center" prop="text" label="名称" />
      <el-table-column align="center" prop="type" label="类型">
        <template #default="scope">
          <el-tag type="info">{{ scope.row.type == 0 ? '文字' : '图片' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="url" label="水印">
        <template #default="scope">
          <el-image :src="scope.row.url" style="max-width: 50px">
            <template #error>
              <div class="image-slot">
                <el-icon><icon-picture /></el-icon>
              </div>
            </template>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="createTime" label="创建日期" />
      <el-table-column align="center" label="操作">
        <template #default="scope">
          <el-button link type="danger" @click="remove(scope.row.id)"> 删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>
  <el-dialog
    v-model="showDialog"
    :title="dialogTitle"
    left
    :append-to-body="true"
    class="drag-dialog small-padding-dialog"
    width="60%"
    :close-on-click-modal="false"
    destroy-on-close
    align-center
  >
    <el-form :model="formData" label-width="100px" ref="formRef">
      <el-form-item
        label="类型"
        :rules="{ required: true, trigger: ['blur', 'change'], message: '请选择水印类型' }"
        prop="type"
      >
        <el-select v-model="formData.type">
          <el-option :value="0" label="文字" />
          <el-option :value="1" label="图片" />
        </el-select>
      </el-form-item>
      <el-form-item :label="formData.type == 0 ? '文字' : '名称'" required prop="text">
        <el-input v-model="formData.text" />
      </el-form-item>
      <el-form-item label="图片" v-if="formData.type == 1" required prop="image">
        <el-upload
          list-type="picture-card"
          :before-upload="beforeAvatarUpload"
          multiple="false"
          :http-request="upload"
        >
          <el-icon><Plus /></el-icon>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import ContentWrap from '@/components/ContentWrap/src/ContentWrap.vue'
import { Plus } from '@element-plus/icons-vue'
import type { UploadFile, UploadProps } from 'element-plus'
import * as watermarkApi from '@/api/watermark'
import * as pictureApi from '@/api/picture'
import * as metaApi from "@/api/meta";

const list = ref([])
const add = () => {
  showDialog.value = true
}
const showDialog = ref(false)
const dialogTitle = ref('添加水印')
const formData = ref({})
const formRef = ref<any>()
const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const disabled = ref(false)
const message = useMessage()
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/png') {
    ElMessage.error('只允许上传png格式水印!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('水印大小不能大于2MB!')
    return false
  }
  return true
}
const upload = (content) => {
  let form = new FormData()
  form.append('file', content.file)
  pictureApi.upload(form).then((res) => {
    formData.value.image = res.data.relativePath
    formData.value.url = res.data.url
  })
}
const remove = async (id) => {
  try {
    await message.confirm('确定删除水印吗？')
    await watermarkApi.remove(id)
    await getList()
  } catch {}
}
const handleRemove = (file: UploadFile) => {}
const save = async () => {
  await watermarkApi.save(formData.value)
  showDialog.value = false
  getList()
}
const getList = async () => {
  list.value = await watermarkApi.list()
}
onMounted(() => {
  getList()
})
</script>
<style scoped>
.avatar-uploader .avatar {
  display: block;
  width: 178px;
  height: 178px;
}
</style>

<style>
.avatar-uploader .el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  width: 178px;
  height: 178px;
  font-size: 28px;
  color: #8c939d;
  text-align: center;
}
</style>
